# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/1/23
# @File   : TX09_IllegalIndex.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:违法违章指数
code:TX09_IllegalIndex
tableName:
columnName:
groups:税务模块
dependencies:TX_CQ_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    '''
    获取表
    :param x:
    :return:
    '''
    try:
        ic_data = TX_CQ_DSJ['data'].get(table_Name)
        if ic_data in null_type_list:
            return None
        else:
            return ic_data if isinstance(ic_data, list) and len(ic_data) > 0 else [{}]
    except:
        return None

def TX09_IllegalIndex():
    try:
        syptWfwzxx = getTable('syptWfwzxx')
        if syptWfwzxx is None:
            return u'缺失值'
        elif syptWfwzxx == [{}]:
            return 0.0
        else:

            flag = []
            #违法违章类型 登记时间+登记类型去重
            type01 = set()
            type02 = set()
            type03 = set()
            type04 = set()
            type05 = set()
            type06 = set()
            type07 = set()
            type08 = set()
            type09 = set()
            type99 = set()

            for i in syptWfwzxx:

                if i.get('DJRQ') not in null_type_list and i.get('WFWZLX_DM') not in null_type_list:
                    if i.get('WFWZLX_DM') == '01':
                        type01.add((i.get('DJRQ') + i.get('WFWZLX_DM')))
                    elif i.get('WFWZLX_DM') == '02':
                        type02.add((i.get('DJRQ') + i.get('WFWZLX_DM')))
                    elif i.get('WFWZLX_DM') == '03':
                        type03.add((i.get('DJRQ') + i.get('WFWZLX_DM')))
                    elif i.get('WFWZLX_DM') == '04':
                        type04.add((i.get('DJRQ') + i.get('WFWZLX_DM')))
                    elif i.get('WFWZLX_DM') == '05':
                        type05.add((i.get('DJRQ') + i.get('WFWZLX_DM')))
                    elif i.get('WFWZLX_DM') == '06':
                        type06.add((i.get('DJRQ') + i.get('WFWZLX_DM')))
                    elif i.get('WFWZLX_DM') == '07':
                        type07.add((i.get('DJRQ') + i.get('WFWZLX_DM')))
                    elif i.get('WFWZLX_DM') == '08':
                        type08.add((i.get('DJRQ') + i.get('WFWZLX_DM')))
                    elif i.get('WFWZLX_DM') == '09':
                        type09.add((i.get('DJRQ') + i.get('WFWZLX_DM')))
                    elif i.get('WFWZLX_DM') == '99':
                        type99.add((i.get('DJRQ') + i.get('WFWZLX_DM')))
                    else:
                        continue
                else:
                    flag.append(None)

            result = 0.4 * len(type01) + 0.6 * len(type02) + 0.6 * len(type03) + 0.0 * len(type04) + 0.1 * len(type05) \
                     + 0.0 * len(type07) + 0.0 * len(type08) + 0.6 * len(type09) + 0.0 * len(type99)

            if len(syptWfwzxx) == len(flag):
                return u'缺失值'
            else:
                return round(result, 4)
    except:
        return u'缺失值'

result = TX09_IllegalIndex()